import Common from '../../common/common.js';
let common = new Common;
const app = getApp()
Page({
  data: {
    loginShow: false,
    imgUrls: [],
    picShopList: [],
    orderType: [],
    orgSeq: '',
    userId: '',
    isIphoneX: '', //判断是否是iphonex
    contentNum: {},
    user: 4, //用户类型(1集团人员2战区人员3基地业务员4店主5店长6店员)
    id: '', //切换店铺返回的shopid
    shopList: [],
    shopInfo: {},
    orgList: [],
    activeNum: 0, //参加活动管理数量
    noticeList: [
      // { 
      //   id:'1',
      //   title:"最新消息",
      //   content:'通知消息描述通知消息描述通知消息描述通知消息描述通知消息描述通知消息描述通知消息描述通知消息描述通知消息描述通知消息描述通知消息描述',
      //   time:'2022.09.09-2020.10.10'
      // }
    ],
    autoplay: true, //判断消息是否自动轮播
  },
  // 获取活动管理数量
  getStoreActivitiesNum: function () {
    let data = {
      orgSeq: app.globalData.orgSeq,
    }
    common.requestAjax('/api/goods-service/v1/goodsShopActivity/getStoreActivitiesNum', 'GET', data).then(res => {
      if (res.data.code == 1) {
        this.setData({
          activeNum: res.data.result
        })
      }
    }).catch(err => {
      console.log(err);
    })
  },
  // banner链接
  gotoDtails(e) {
    let item = e.currentTarget.dataset.item;
    let posterUrl = item.posterUrl;
    let goodsId = item.goodsId;
    let skipType = item.skipType;
    let uri = item.uri
    if (skipType == 3) {
      wx.navigateTo({
        url: "/pages/shop/slotMachines/slotMachines",
      })
      return false
    }
    wx.navigateTo({
      url: '/pages/index/indexDetil/index?posterUrl=' + posterUrl + '&goodsId=' + goodsId + '&skipType=' + skipType + '&uri=' + uri,
    })
  },
  // 跳转优惠券
  // coupons:function(e){
  //   wx.navigateTo({
  //     url: '/pages/couponsMgt/index',
  //   })
  // },
  changeShop: function () {
    let routeUrl = '/pages/index/chooseShop/index';
    let routeType = 'navigateTo';
    let params = '';
    common.routeLink(routeUrl, params, routeType)
  },
  alldata: function () {
    wx.navigateTo({
      url: '/pages/dataPage/index?typeIndex=' + 2,
    })
  },
  paySure: function (e) {
    let routeUrl = '/pages/myCenter/reconciliation/reconciliation';
    let routeType = 'navigateTo';
    let params = '';
    common.routeLink(routeUrl, params, routeType)
  },
  naviTo: function (e) {
    let routeUrl = e.currentTarget.dataset.url;
    if (routeUrl == '/pages/rightsMgt/rootEmployees/rootEmployees') {
      wx.navigateTo({
        url: '/pages/rightsMgt/rootEmployees/rootEmployees?accessMode=2'
      })
    } else {
      wx.navigateTo({
        url: routeUrl,
      })
    }
  },
  // 订单类别
  orderTo: function (e) {
    let type = e.currentTarget.dataset.type;
    let routeUrl = '/pages/orderMgt/index';
    let routeType = 'navigateTo';
    let params = '?type=' + type;
    common.routeLink(routeUrl, params, routeType)
  },
  // 轮播图和按钮
  getList: function (e) {
    common.requestAjax('/api/promotion-service/v1/PageConfig/queryShopConfigurationInfor', 'GET', ).then(res => {
      if (res.data.code == 1) {
        let shopList = res.data.result.shopList;
        this.setData({
          imgUrls: shopList,
        })
        wx.setStorageSync('videoUrl', res.data.result.picShopList[0].picUrl)
      }
    }).catch(err => {
      console.log(err);
    })
  },
  getOrderNumDy() {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if (month < 10) {
      month = "0" + month;
    }
    if (day < 10) {
      day = "0" + day;
    }
    var nowDate = year + "-" + month + "-" + day;
    // 用户类型(1集团人员2战区人员3基地业务员4店主5店长6店员)
    let data = {
      orgSeq: this.data.orgSeq,
      userId: '',
      queryDay: nowDate
    }
    common.requestAjax('/api/trade-service/v1/tradeInfo/queryDayBusiness', 'GET', data).then(res => {
      if (res.data.code == 1) {
        this.setData({
          contentNum1: res.data.result
        })
      }
    }).catch(err => {
      console.log(err);
    })
  },
  // 获取经营数据
  getOrderNum: function (e) {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if (month < 10) {
      month = "0" + month;
    }
    if (day < 10) {
      day = "0" + day;
    }
    var nowDate = year + "-" + month + "-" + day;
    // 用户类型(1集团人员2战区人员3基地业务员4店主5店长6店员)
    let data = {
      orgSeq: this.data.orgSeq,
      userId: '',
      queryDay: nowDate
    }
    if (this.data.user == 6) {
      data.userId = this.data.userId
    }
    common.requestAjax('/api/trade-service/v1/tradeInfo/queryDayBusiness', 'GET', data).then(res => {
      if (res.data.code == 1) {
        this.setData({
          contentNum: res.data.result
        })
      }
    }).catch(err => {
      console.log(err);
    })
  },
  // 分订单状态数
  getOrderType: function (e) {
    let userType = app.globalData.shopInfo.userType;
    let data = {};
    if (userType == 6) {
      data = {
        orgSeq: this.data.orgSeq,
        userId: this.data.userId,
      }
    } else {
      data = {
        orgSeq: this.data.orgSeq,
        userId: '',
      }
    }
    common.requestAjax('/api/trade-service/v1/tradeInfo/queryTradeStatusNum', 'GET', data).then(res => {
      if (res.data.code == 1) {
        let arrayList = [0, 0, 0, 0, 0];
        res.data.records.forEach((item, index) => {
          arrayList[item.orderStatus] = item.orderNum;
        })
        this.setData({
          orderType: arrayList
        })
      }
    }).catch(err => {
      console.log(err);
    })
  },
  onShow() {
    if (wx.getStorageSync('isLogin')) {
      this.setData({
        loginShow: false,
      })
      common.checkLogin().then((res) => {
        console.log('已登陆');
        this.getPageData();
        common.addAppData(res.data.result.userId, res.data.result.orgList[0].orgSeq)
      }).catch(err => {
        console.log(err)
      });
    } else {
      this.setData({
        loginShow: true
      })
      wx.showModal({
        title: '提示',
        content: '您尚未登录,该管理工具需登录后才能操作,请点击导航栏-个人中心进行登录',
        showCancel: false,
        confirmText: '我知道了',
        success: res => {
          console.log(res)
        }
      })
    }
  },
  onLoad(options) {
    this.getList() //获取轮播图 
    this.queryUserMessage() //获取消息通知列表
  },
  // 获取消息通知列表
  queryUserMessage() {
    let data = {
      "size": 1000,
      "current": 1,
      "condition": {
        'status': 1,
        'orgSeq': this.data.orgSeq
      }
    }
    common.requestAjax('/api/user-service/v1/userMessage/queryMessageByOrgSeq', 'POST', data).then(res => {
      if (res.data.code == 1) {
        if (res.data.records.length == 1) {
          this.setData({
            autoplay: false,
          })
        } else {
          this.setData({
            autoplay: true,
          })
        }
        this.setData({
          noticeList: res.data.records,
        })
      }
    })
  },
  gotoLogin() {
    wx.showModal({
      title: '提示',
      content: '您尚未登录,该管理工具需登录后才能操作,请点击导航栏-个人中心进行登录',
      showCancel: false,
      confirmText: '我知道了',
      success: res => {
        console.log(res)
      }
    })
  },
  //渲染数据
  getPageData() {
    app.globalData.orgSeq = app.globalData.orgSeq || app.globalData.defaultShopInfo.orgSeq;
    app.globalData.shopInfo = app.globalData.shopInfo || app.globalData.defaultShopInfo;
    let isIphoneX = app.globalData.isIphoneX;
    // console.log('==' + JSON.stringify(app.globalData))
    this.setData({
      isIphoneX: isIphoneX,
      orgSeq: app.globalData.orgSeq,
      userId: app.globalData.userId,
      user: app.globalData.shopInfo.userType,
      shopInfo: JSON.parse(JSON.stringify(app.globalData.shopInfo)),
      orgList: JSON.parse(JSON.stringify(app.globalData.orgList)),
    })
    this.getRootData(); //获取权限
    this.getOrderType() //订单数量
    this.getOrderNum(); //获取经营数据
    this.getStoreActivitiesNum() //获取活动管理数量
    //店员还有个本店数据
    if (app.globalData.shopInfo.userType == 6) {
      this.getOrderNumDy()
    }
    console.log(JSON.stringify(this.data.shopInfo))
    console.log(this.data.orgList, '店铺列表')
  },
  getRootData() {
    let shopInfo = app.globalData.shopInfo;
    let data = {
      userType: shopInfo.userType,
      orgSeq: shopInfo.orgSeq,
      userId: app.globalData.userId,
    }
    common.requestAjax('/api/user-service/v1/detail/getAuthority', 'POST', data).then(res => {
      if (res.data.code) {
        let authorityList = res.data.result.authorityList.filter(item => {
          if (item.isShow != 0) return item
        })
        this.setData({
          picShopList: authorityList
        })
      }
    }).catch(err => {})
  },
  gotoDataPage(e) {
    if (this.data.user != 6) {
      wx.navigateTo({
        url: '/pages/dataPage/index?pageType=' + e.currentTarget.dataset.type,
      })
    }
  },
  // 消息列表
  inforList() {
    wx.navigateTo({
      url: '/pages/infor/index',
    })
  },
  // 常见问题
  commonProblem: function (e) {
    wx.navigateTo({
      url: '/pages/myCenter/commonProblem/commonProblem',
    })
  },
  // 远程图片加载404显示默认图
  onImageError: function (ev) {
    //console.log("onImageError",ev);
    var _that = this;
    app.errImgFun(ev, _that);
  }
})